﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using Collab.Common.Contacts;
using Collab.Services.Remote.WebserviceProxy;
using Collab.Services.Remote.WebserviceProxy.ServiceProxy;
using Microsoft.Practices.Composite.Presentation.Commands;


namespace Collab.Modules.ContactBrowser.ViewModel
{
    public class SearchContactViewModel : ViewModel
    {
        #region Fields and properties

        private Contact selectedUser;
        public Contact SelectedUser
        {
            get
            {
                return this.selectedUser;
            }
            set
            {
                this.selectedUser = value;
            }
        }

        private ObservableCollection<Contact> users;
        public ObservableCollection<Contact> Users
        {
            get
            {
                return this.users;
            }
            set
            {
                this.users = value;
            }
        }
        private ContactsViewModel ContactViewModel;
        private IRepository repository;

        private DelegateCommand<string> searchUsersCommand;
        public DelegateCommand<string> SearchUsersCommand
        {
            get
            {
                return this.searchUsersCommand;
            }
            set
            {
                if (this.searchUsersCommand != value)
                {
                    this.searchUsersCommand = value;
                    NotifyPropertyChanged("SearchUsersCommand");
                }
            }
        }
        
        private ICommand addContactCommand;
        public ICommand AddContactCommand
        {
            get
            {
                return this.addContactCommand;
            }
            set
            {
                if (this.addContactCommand != value)
                {
                    this.addContactCommand = value;
                    NotifyPropertyChanged("AddContactCommand");
                }
            }
        }

        private ICommand inviteToGroupCommand;
        public ICommand InviteToGroupCommand
        {
            get
            {
                return this.inviteToGroupCommand;
            }
            set
            {
                if (this.inviteToGroupCommand != value)
                {
                    this.inviteToGroupCommand = value;
                    NotifyPropertyChanged("InviteToGroupCommand");
                }
            }
        }

        public event EventHandler SearchUsersCompleted;
        public event EventHandler ContactAdded;

        #endregion

        public SearchContactViewModel(IRepository repository, ContactsViewModel viewModel)
        {
            this.repository = repository;
            ContactViewModel = viewModel;

            //Init commands
            SearchUsersCommand = new DelegateCommand<string>(SearchUsers);
            AddContactCommand = new DelegateCommand<object>(param => AddToContacts());
            

            //Init callback
            SearchUsersCompleted += new EventHandler(SearchContactViewModel_SearchUsersCompleted);
            ContactAdded += new EventHandler(SearchContactViewModel_ContactAdded);
        }

        public void SearchUsers(string query)
        {
            repository.SearchUsersAsync(query, SearchUsersCompleted);
        }
        public void SearchContactViewModel_SearchUsersCompleted(object sender, EventArgs e)
        {
            CallbackResult<ObservableCollection<Contact>> result = (CallbackResult<ObservableCollection<Contact>>)sender;
            if (!result.IsError)
            {
                users = result.Object;
                NotifyPropertyChanged("Users");
            }
            else
            {
                // TODO error to view
                // result.ErrorMessage;
            }
        }

        public void AddToContacts()
        {
            repository.AddToConactsAsync(ContactViewModel.CurrentUser, SelectedUser, ContactAdded);
        }
        public void SearchContactViewModel_ContactAdded(object sender, EventArgs e)
        {
            CallbackResult<Contact> result = (CallbackResult<Contact>)sender;
            if (!result.IsError)
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        ContactViewModel.Groups.First().Users.Add(result.Object);
                        NotifyPropertyChanged("Groups");
                    }));
            }
            else
            {
                
                System.Windows.Forms.MessageBox.Show("Contact could not be added: " + result.ErrorMessage);

            }
        }

        

    }
}
